<template>
  <!-- 供应商/客户表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="700"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="110">
      <el-row>
        <el-col :span="12">
          <el-form-item label="公司编号" prop="partnerCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.partnerCode"
              placeholder="请输入公司编号"
              clearable
            />

            <span v-else>{{ formData.partnerCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公司名" prop="partnerName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.partnerName"
              placeholder="请输入公司名"
              clearable
            />

            <span v-else>{{ formData.partnerName }}</span>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="机构;基本信息" prop="organizationId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.organizationId"
              placeholder="请输入机构;基本信息"
              clearable
            />

            <span v-else>{{ formData.organizationId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机构代码;基本信息" prop="organizationCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.organizationCode"
              placeholder="请输入机构代码;基本信息"
              clearable
            />

            <span v-else>{{ formData.organizationCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机构名称;基本信息" prop="organizationName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.organizationName"
              placeholder="请输入机构名称;基本信息"
              clearable
            />

            <span v-else>{{ formData.organizationName }}</span>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="地址描述" prop="addressDesc">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.addressDesc"
              placeholder="请输入地址描述"
              clearable
            />

            <span v-else>{{ formData.addressDesc }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="国家" prop="country">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.country"
              placeholder="请输入国家"
              clearable
            />

            <span v-else>{{ formData.country }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="省" prop="province">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.province"
              placeholder="请输入省"
              clearable
            />

            <span v-else>{{ formData.province }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="城市" prop="city">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.city"
              placeholder="请输入城市"
              clearable
            />

            <span v-else>{{ formData.city }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地区" prop="region">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.region"
              placeholder="请输入地区"
              clearable
            />

            <span v-else>{{ formData.region }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="街道" prop="street">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.street"
              placeholder="请输入街道"
              clearable
            />

            <span v-else>{{ formData.street }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="门牌号" prop="houseCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.houseCode"
              placeholder="请输入门牌号"
              clearable
            />

            <span v-else>{{ formData.houseCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮政编码" prop="zipCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.zipCode"
              placeholder="请输入邮政编码"
              clearable
            />

            <span v-else>{{ formData.zipCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人1" prop="contactName1">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.contactName1"
              placeholder="请输入联系人1"
              clearable
            />

            <span v-else>{{ formData.contactName1 }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人1电话" prop="contactPhone1">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.contactPhone1"
              placeholder="请输入联系人1电话"
              clearable
            />

            <span v-else>{{ formData.contactPhone1 }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人2" prop="contactName2">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.contactName2"
              placeholder="请输入联系人2"
              clearable
            />

            <span v-else>{{ formData.contactName2 }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人2电话" prop="contactPhone2">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.contactPhone2"
              placeholder="请输入联系人2电话"
              clearable
            />

            <span v-else>{{ formData.contactPhone2 }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.remarks"
              type="textarea"
              placeholder="请输入备注"
              clearable
            />

            <span v-else>{{ formData.remarks }}</span>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="创建时间" prop="createTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createTime"
              placeholder="请输入创建时间"
              clearable
            />

            <span v-else>{{ formData.createTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建人" prop="createdUserId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createdUserId"
              placeholder="请输入创建人"
              clearable
            />

            <span v-else>{{ formData.createdUserId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新时间" prop="lastUpdateTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdateTime"
              placeholder="请输入更新时间"
              clearable
            />

            <span v-else>{{ formData.lastUpdateTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新人" prop="lastUpdatedUserId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdatedUserId"
              placeholder="请输入更新人"
              clearable
            />

            <span v-else>{{ formData.lastUpdatedUserId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="删除标识" prop="deleted">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.deleted"
              placeholder="请输入删除标识"
              clearable
            />

            <span v-else>{{ formData.deleted }}</span>
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "NPMMDMPARTNERADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  partnerCode: [
    {
      required: true,
      pattern: "[^ \x20]+",
      message: "请输入公司编号",
      trigger: "blur",
    },
    {
      required: true,
      message: "请输入公司编号",
      trigger: "blur",
    },
    { min: 6, max: 6, message: "公司编号必须为6位", trigger: "blur" },
  ],
  partnerName: [{ required: true, message: "请填写公司名称", trigger: "blur" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  partnerCode: "",
  partnerName: "",
  organizationId: "",
  organizationCode: "",
  organizationName: "",
  addressDesc: "",
  country: "",
  province: "",
  city: "",
  region: "",
  street: "",
  houseCode: "",
  zipCode: "",
  contactName1: "",
  contactPhone1: "",
  contactName2: "",
  contactPhone2: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        partnerCode,
        partnerName,
        organizationId,
        organizationCode,
        organizationName,
        addressDesc,
        country,
        province,
        city,
        region,
        street,
        houseCode,
        zipCode,
        contactName1,
        contactPhone1,
        contactName2,
        contactPhone2,
        remarks,
        createTime,
        createdUserId,
        lastUpdateTime,
        lastUpdatedUserId,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        partnerCode,
        partnerName,
        organizationId,
        organizationCode,
        organizationName,
        addressDesc,
        country,
        province,
        city,
        region,
        street,
        houseCode,
        zipCode,
        contactName1,
        contactPhone1,
        contactName2,
        contactPhone2,
        remarks,
        createTime,
        createdUserId,
        lastUpdateTime,
        lastUpdatedUserId,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.partnerCode = "";
  formData.partnerName = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.organizationName = "";
  formData.addressDesc = "";
  formData.country = "";
  formData.province = "";
  formData.city = "";
  formData.region = "";
  formData.street = "";
  formData.houseCode = "";
  formData.zipCode = "";
  formData.contactName1 = "";
  formData.contactPhone1 = "";
  formData.contactName2 = "";
  formData.contactPhone2 = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 供应商/客户保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/wms/mdmpartner/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
